package com.hogan.hoganproject.contacts.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.hogan.hoganproject.R;
import com.hogan.hoganproject.pinyin.Util;
import com.hogan.hoganproject.view.ContactsHeadView;

import java.util.List;



public class ContactsAdapter extends BaseAdapter {

    private Context mContext;

    private List<ContactsItemBean> mContactsListData;


    public ContactsAdapter(Context context, List<ContactsItemBean> contactsListData) {
        this.mContext = context;
        this.mContactsListData = contactsListData;
    }

    @Override
    public int getCount() {
        return mContactsListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mContactsListData.size() == 0 ? null : mContactsListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        ContactsItemBean contactsItemBean = mContactsListData.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.contacts_item_layout, null);
            viewHolder.indexTextView = (TextView) convertView.findViewById(R.id.index);
            viewHolder.userNameTextView = (TextView) convertView.findViewById(R.id.username);
            viewHolder.mHeadView = convertView.findViewById(R.id.head_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.userNameTextView.setText(contactsItemBean.getUserName());
        if (!TextUtils.isEmpty(getHeadViewFirstName(contactsItemBean.getUserName()))) {
            viewHolder.mHeadView.setHeadName(getHeadViewFirstName(contactsItemBean.getUserName()));
        } else {
            viewHolder.mHeadView.setHeadName("*");
        }
        char index = Util.getIndex(contactsItemBean.getUserName());
        if (position == 0 || Util.getIndex(mContactsListData.get(position - 1).getUserName()) != index) {
            viewHolder.indexTextView.setVisibility(View.VISIBLE);
            viewHolder.indexTextView.setText(String.valueOf(index));
        } else {
            viewHolder.indexTextView.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        TextView indexTextView;
        TextView userNameTextView;
        ContactsHeadView mHeadView;
    }

    private String getHeadViewFirstName(String name) {
        String curName = "";
        if (TextUtils.isEmpty(name)) {
            return curName;
        }
        int index = name.length() - 1;
        for (int i = index; i >= 0; i--) {
            char firstStr = name.charAt(i);
            if (isPinyin(String.valueOf(firstStr))) {
                curName = String.valueOf(firstStr);
                return curName;
            }
        }
        return curName;
    }


    private boolean isPinyin(String src) {
        String res = src;
        try {
            res = PinyinHelper.convertToPinyinString(src, "", PinyinFormat.WITHOUT_TONE).toUpperCase();
        } catch (Exception e) {
        }
        if (res.charAt(0) >= 'A' && res.charAt(0) <= 'Z') {
            return true;
        } else {
            return false;
        }
    }
}
